package com.leetcode.LC_MED;

import com.review.test.Solution;

public class LC50 {
    public double myPow(double x, int n) {
        long p = n;
        p = Math.abs(p);
        double temp = x;
        double result = 1;
        while (p > 0) {
            if ((p & 1) == 1) {//如果是奇数
                result = result * temp;
            }
            temp = temp * temp;
            p >>= 1;//每次指数变为原来的二倍
        }
        return n > 0 ? result : 1.0 / result;
    }

    public double myPow2(double x, int n) {
        long N = n;
        return N >= 0 ? quickMul(x, N) : 1.0 / quickMul(x, -N);
    }

    public double quickMul(double x, long N) {
        if (N == 0) {
            return 1.0;
        }
        double y = quickMul(x, N / 2);
        return N % 2 == 0 ? y * y : y * y * x;
    }

    public static void main(String[] args) {
        LC50 lc50 = new LC50();
        double pow = lc50.myPow2(34.00515, -3);
        System.out.println(pow);
    }
}
